package com.ruoyi.sample.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.attachedData.domain.AttachedData;
import com.ruoyi.attachedRecord.domain.AttachedRecord;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.engineering.domain.Engineering;
import com.ruoyi.engineering.service.IEngineeringService;
import com.ruoyi.notice.domain.Notice;
import com.ruoyi.notice.service.INoticeService;
import com.ruoyi.sampleRecord.domain.SampleRecord;
import com.ruoyi.sampleRecord.mapper.SampleRecordMapper;
import com.ruoyi.sampleRecord.service.ISampleRecordService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.task.domain.Task;
import com.ruoyi.task.service.ITaskService;
import com.ruoyi.workload.domain.Workload;
import com.ruoyi.workload.mapper.WorkloadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sample.mapper.SampleMapper;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;

import javax.annotation.Resource;

/**
 * 实验样品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@Service
public class SampleServiceImpl implements ISampleService
{
    @Autowired
    private SampleMapper sampleMapper;

    @Resource
    private SampleRecordMapper sampleRecordMapper;

    @Resource
    private WorkloadMapper workloadMapper;

    @Autowired
    private ITaskService taskService;

    @Autowired
    private ICommissionService commissionService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 查询实验样品
     *
     * @param id 实验样品主键
     * @return 实验样品
     */
    @Override
    public Sample selectSampleById(Long id)
    {
        return sampleMapper.selectSampleById(id);
    }


    @Override
    public List<Sample> selectSampleListCount(Sample sample){
        List<Sample> sampleDataList = sampleMapper.selectSampleListCount(sample);
        return sampleDataList;
    }
    /**
     * 查询实验样品列表
     *
     * @param sample 实验样品
     * @return 实验样品
     */
    @Override
    public List<Sample> selectSampleList(Sample sample)
    {
        List<Sample> sampleDataList = sampleMapper.selectSampleList(sample);
        if(!sampleDataList.isEmpty()) {
            SampleRecord attachedRecord = new SampleRecord();
            for (Sample attachedData1 : sampleDataList) {
                Integer completionNumber = 0;
                attachedRecord.setSampleId(attachedData1.getId());
                List<SampleRecord> attachedRecordList = sampleRecordMapper.selectSampleRecordList(attachedRecord);
                if (!attachedRecordList.isEmpty()) {
                    for (SampleRecord attachedData2 : attachedRecordList) {
                        if(StringUtils.isNotEmpty(attachedData2.getSampleNumber())){
                            completionNumber += Integer.parseInt(attachedData2.getSampleNumber());
                        }
                    }
                }
                attachedData1.setCompletionProgress(String.valueOf(completionNumber));

            }
        }
        return sampleDataList;
    }

    /**
     * 新增实验样品
     *
     * @param sample 实验样品
     * @return 结果
     */
    @Override
    public int insertSample(Sample sample)
    {
        sample.setCreateTime(DateUtils.getNowDate());
        Engineering engineeringOne = engineeringService.selectEngineeringById(sample.getEngineeringId());
        sample.setEnterpriseId(engineeringOne.getEnterpriseId());
        int size = sampleMapper.insertSample(sample);

        //任务增加一条（试验人 不为空）
        if(sample.getChargePerson() != null){
            List<SysDictData> ddList = dictDataService.selectDictDataByDictValue(sample.getProject());
            String projectName = ddList.get(0).getDictLabel();

            Commission commissionOne = commissionService.selectCommissionById(sample.getCommissionId());
            Task task = new Task();
            task.setCommissionId(sample.getCommissionId());
            task.setEngineeringId(sample.getEngineeringId());
            task.setTaskType("检测数据录入");
            task.setStatus("0");
            task.setTaskContent(commissionOne.getEngineeringName() + "的委托单" + commissionOne.getNumber() +"的试验项目："+projectName+"已分配，请及时检测。");
            task.setChargePerson(sample.getChargePerson());
            taskService.insertTask(task);

            //更新之前任务
            Task task1 = new Task();
            task1.setTaskType("分配试验项目");
            task1.setCommissionId(sample.getCommissionId());
            List<Task> taskList = taskService.selectTaskList(task1);
            if(taskList.size() > 0){
                task1.setId(taskList.get(0).getId());
                task1.setStatus("1");
                task1.setExecuteTime(DateUtils.getNowDate());
                taskService.updateTask(task1);
            }

            //消息
            //检测通知 给 试验员
            Notice notice = new Notice();
            notice.setCommissionId(commissionOne.getId());
            notice.setEngineeringId(commissionOne.getEngineeringId());
            notice.setNoticType("检测通知");
            notice.setNoticContent(commissionOne.getEngineeringName()+"的委托单"+commissionOne.getNumber()+"已受理,请及时检测");
            notice.setUserId(sample.getChargePerson());
            noticeService.insertNotice(notice);
        }

        return size;
    }

    /**
     * 修改实验样品
     *
     * @param sample 实验样品
     * @return 结果
     */
    @Override
    public int updateSample(Sample sample)
    {
        Sample sampleOne = sampleMapper.selectSampleById(sample.getId());
        if(sampleOne.getChargePerson() == null || !(sampleOne.getChargePerson().equals(sample.getChargePerson())) ){


            List<SysDictData> ddList = dictDataService.selectDictDataByDictValue(sampleOne.getProject());
            String projectName = ddList.get(0).getDictLabel();

            Commission commissionOne = commissionService.selectCommissionById(sampleOne.getCommissionId());
            Task task = new Task();
            task.setCommissionId(sampleOne.getCommissionId());
            task.setEngineeringId(sampleOne.getEngineeringId());
            task.setTaskType("检测数据录入");
            task.setStatus("0");
            task.setTaskContent(commissionOne.getEngineeringName() + "的委托单" + commissionOne.getNumber() +"的试验项目："+projectName+"已分配，请及时检测。");
            task.setChargePerson(sampleOne.getChargePerson());
            taskService.insertTask(task);

            //更新之前任务
            Task task1 = new Task();
            task1.setTaskType("分配试验项目");
            task1.setCommissionId(sampleOne.getCommissionId());
            List<Task> taskList = taskService.selectTaskList(task1);
            if(taskList.size() > 0){
                task1.setId(taskList.get(0).getId());
                task1.setStatus("1");
                task1.setExecuteTime(DateUtils.getNowDate());
                taskService.updateTask(task1);
            }

            //消息
            //检测通知 给 试验员
            Notice notice = new Notice();
            notice.setCommissionId(commissionOne.getId());
            notice.setEngineeringId(commissionOne.getEngineeringId());
            notice.setNoticType("检测通知");
            notice.setNoticContent(commissionOne.getEngineeringName()+"的委托单"+commissionOne.getNumber()+"已受理,请及时检测");
            notice.setUserId(sample.getChargePerson());
            noticeService.insertNotice(notice);

        }

        sample.setUpdateTime(DateUtils.getNowDate());
        return sampleMapper.updateSample(sample);
    }


    @Override
    public int updateSampleForStatus(Long commissionId)
    {
        return sampleMapper.updateSampleForStatus(commissionId);
    }

    /**
     * 批量删除实验样品
     *
     * @param ids 需要删除的实验样品主键
     * @return 结果
     */
    @Override
    public int deleteSampleByIds(Long[] ids)
    {
        return sampleMapper.deleteSampleByIds(ids);
    }


    /**
     * 批量逻辑删除实验样品
     *
     * @param ids 需要删除的实验样品主键
     * @return 结果
     */
    @Override
    public int logicDeleteSample(Long[] ids,String delType,Long delUser)
    {
        return sampleMapper.logicDeleteSample(ids,delType,delUser);
    }

    /**
     * 删除实验样品信息
     *
     * @param id 实验样品主键
     * @return 结果
     */
    @Override
    public int deleteSampleById(Long id)
    {
        return sampleMapper.deleteSampleById(id);
    }
}
